package com.server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.CustomerHelper;
import com.Helper.FlightHelper;
import com.Helper.FlightInfo;
import com.Helper.PaymentHelper;
import com.Helper.ReservationHelper;
import com.Helper.ReservationInfo;
import com.Helper.SeatHelper;
import com.Helper.SeatInfo;


public class CheckModServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	String msg = null;

	public String GetSeatType(String seatID)
	{
		
		String seatNum = seatID.substring(1, 3);
		String type = null;
		int num = Integer.parseInt(seatNum);
		
		if (num >= 10 && num <= 11)
		{
			type = "Business";
		}
		else if (num >= 12 && num <= 18)
		{
			type = "Economy Plus";
		}
		else if (num >= 19 && num <= 30)
		{
			type = "Economy";
		}
		else
		{
			System.out.println("invalid SeatID");
		}
		
		return type;
	}
	
	private boolean IsDowngrade(String newSeatID, SeatInfo oldSeatInfo)
	{
		String oldType = oldSeatInfo.getType();
		String newType = GetSeatType(newSeatID);
		
		// is downgrade true
		// is not false
		
		if (oldType.equals("Business"))
		{
			return true;
		}
		else if (oldType.equals("Economy Plus"))
		{
			if (newType.equals("Economy Plus") || newType.equals("Economy"))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else if (oldType.equals("Economy"))
		{
			if (newType.equals("Economy"))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			System.out.println("you shouldn't see this");
			return false;
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		String username = req.getParameter("username");
		String resID = req.getParameter("resID");
		String firstName = req.getParameter("firstName");
		String lastName = req.getParameter("lastName");
		String seatID = req.getParameter("seatID");
		String meal = req.getParameter("mealOption");
		String flightID = req.getParameter("flightID");
		String msg = null;
		
		float price = 0f;
		
		try 
		{
			ReservationHelper.Setup(connection, username);
			SeatHelper.Setup(connection);
			PaymentHelper.Setup(connection);
			
			ReservationInfo oldResInfo = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID));
			SeatInfo oldSeatInfo = SeatHelper.GetSelectedSeat(oldResInfo.seat, oldResInfo.flightID);
			
			if (IsDowngrade(seatID, oldSeatInfo) ==true )
			{
				int id = Integer.parseInt(resID);
				ReservationHelper.ModFirstName(firstName, id);
				ReservationHelper.ModLastName(lastName, id);
				ReservationHelper.ModMeal(meal, id);
				msg = "Reservation ID " + id + " has been modified.";
				
				ReservationHelper.ModSeat(seatID, Integer.parseInt(resID));
				SeatHelper.MarkEmpty(oldResInfo.seat, oldResInfo.flightID);
				SeatHelper.MarkReserved(seatID, flightID);	
				
				req.setAttribute("username", username);
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("reservation/ModifiedResult.jsp").forward(req, resp);
			}
			else
			{
				String newClass = GetSeatType(seatID);
				String oldClass = GetSeatType(oldResInfo.seat);
				
				if (oldClass.equals("Economy") && newClass.equals("Economy Plus"))
				{
					price = 50.f;
				}
				else if (oldClass.equals("Economy") && newClass.equals("Business"))
				{
					price = 100.f;
				}
				else if (oldClass.equals("Economy Plus") && newClass.equals("Business"))
				{
					price = 50.f;
				}
				else
				{
					System.out.println("Something happened in CheckModServlet");
					price = 0f;
				}
				
				req.setAttribute("username", username);
				req.setAttribute("resID", resID);
				req.setAttribute("extra_price", price);
				req.setAttribute("seatID", seatID);
				req.setAttribute("flightID", flightID);
				req.getRequestDispatcher("reservation/mod_payment.jsp").forward(req, resp);
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
		
	}
}

